<template>
	<el-card class="form-container">
		<el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()" label-width="90px">
			<el-form-item label="品牌名称" prop="brandName">
				<el-input v-model="dataForm.brandName" placeholder="请输入品牌名称"></el-input>
			</el-form-item>
			<el-form-item label="品牌图片" prop="brandImageUrl">
				<el-upload
					class="avatar-uploader"
					action="#"
					:show-file-list="false"
					:on-success="handleAvatarSuccess"
					:before-upload="beforeAvatarUpload"
					:auto-upload="true"
					:data="brandImage"
					:http-request="uploadImage">
					<img v-if="dataForm.brandImageUrl" :src="dataForm.brandImageUrl" class="avatar">
					<i v-else class="el-icon-plus avatar-uploader-icon"></i>
				</el-upload>
			</el-form-item>
			<el-form-item label="首字母" prop="brandAcronym">
				<el-input v-model="dataForm.brandAcronym" placeholder="请输入品牌首字母"></el-input>
			</el-form-item>
			<el-form-item label="品牌排序" prop="brandSort">
				<el-input v-model="dataForm.brandSort" placeholder="请输入品牌排序"></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="roleStatus">
				<el-radio-group v-model="dataForm.brandStatus">
					<el-radio label="1">启用</el-radio>
					<el-radio label="2">禁用</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item>
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="submit('dataForm')">确定</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
	import { addGoodsBrand } from '@/api/mall/goodsBrand'
	import { uploadImage } from '@/api/common/upload'
	export default {
		name: 'add-goodsBrand',
		data() {
			return {
				dataForm: {
					brandName: '',
					brandImageUrl: '',
					brandAcronym: '',
					brandSort: '',
					brandStatus: '',
				},
				brandImage:{
                    uploadType:'eyeglass-user'
                },
				dataRule: {
					brandName: [{
							required: true,
							message: '请输入品牌名称',
							trigger: 'blur'
						},
					],
					brandImageUrl: [{
							required: true,
							message: '请输入品牌图片:存储图片路径',
							trigger: 'blur'
						},
					],
					brandAcronym: [{
							required: true,
							message: '请输入品牌首字母',
							trigger: 'blur'
						},
					],
					brandSort: [{
							required: true,
							message: '请输入品牌排序',
							trigger: 'blur'
						},
					],
					brandStatus: [
						{ required: true, message: '请选择状态',trigger: 'blur'}
					],
				}
			}
		},
		methods: {
			cancel() {
				this.$router.go(-1)
			},
			submit() {
				const set = this.$refs;
				set['dataForm'].validate((valid) => {
					if (!valid) {
						return false
					}
					addGoodsBrand(this.dataForm).then(() => {
						this.$notify({
							title: '添加成功',
							message: '添加成功',
							type: 'success',
							duration: 2000,
							onClose: this.$router.go(-1)
						})
					})
				})
			},
			beforeAvatarUpload(file) {
				const isJPG = file.type === 'image/jpeg';
				const isLt2M = file.size / 1024 / 1024 < 2;
				if (!isJPG) {
					this.$message.error('上传头像图片只能是 JPG 格式!');
				}
				if (!isLt2M) {
					this.$message.error('上传头像图片大小不能超过 2MB!');
				}
				return isJPG && isLt2M;
			},
			handleAvatarSuccess(res, file) {
				this.dataForm.brandImageUrl = URL.createObjectURL(file.raw);
			},
			uploadImage(val){
				let formData = new FormData();
				formData.append('uploadType',this.brandImage.uploadType)
				formData.append('files',val.file)
				uploadImage(formData).then(res =>{
					this.dataForm.brandImageUrl = res.data.responseData[0]
				})
			}
		}
	}
</script>

<style>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
	border-color: #409EFF;
  }
  .avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
  }
  .avatar {
	width: 178px;
	height: 178px;
	display: block;
  }
</style>
